import {
    Component,
    OnInit,
    Injector,
    ViewChild,
    Output,
    EventEmitter,
    ElementRef,
    Input,
    ViewEncapsulation
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    StockZT_OutBillItemServiceProxy,
    ListDtoExtensionsItem,
    ComboboxItemDto,
    StockZT_AccountBaseServiceProxy,
    StockType,
    Stock_AccountBaseListDto,
    StockZT_RoomServiceProxy,
    StockZT_IntoBillItemServiceProxy,
    StockZT_ToolingAccountServiceProxy,
    LabelValue
} from '@shared/service-proxies/stock-service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import { LazyLoadEvent } from 'primeng/api';
import { Paginator } from 'primeng/primeng';
import { Table } from 'primeng/table';
import { appModuleAnimation } from '@shared/animations/routerTransition';
// import {
//     CostZT_ProdQCCardServiceProxy,
//     Cost_ProdQCCardListDto,
//     ListResultDtoOfBomDto
// } from '@shared/service-proxies/cost-service-proxies';
import { UserDataPermissionServiceProxy } from '@shared/service-proxies/stock-service-proxies';
import * as _ from 'lodash';

@Component({
    selector: 'toolingAccountBaseInfoModal',
    templateUrl: './tooling-account-base-info-modal.component.html',
    animations: [appModuleAnimation()],
    encapsulation: ViewEncapsulation.None
})
export class ToolingAccountBaseInfoModalComponent extends AppComponentBase implements OnInit {
    constructor(
        injector: Injector,
        // private _costZT_TaskServiceProxy: CostZT_ProdQCCardServiceProxy,
        private _accountService: StockZT_ToolingAccountServiceProxy,
        private _userDataPermissionServiceProxy: UserDataPermissionServiceProxy,
        private _stockZT_RoomServiceProxy: StockZT_RoomServiceProxy,
        private _billService: StockZT_IntoBillItemServiceProxy
    ) {
        super(injector);
    }

    @Input() set handletype(val) {
        if (val) {
            this._handletype = val;
        }
    }

    @ViewChild('createOrEditModal') modal: ModalDirective;
    @ViewChild('createOrEditModal') modalEl: ElementRef;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;

    _handletype: string;
    anyncing = false;
    f_MaterialCode = '';
    f_MaterialName = '';
    f_CAL_ToolingPictureId = '';
    f_Station = '';
    f_TecCondition = '';
    f_Specification = '';
    f_InventBatchID = '';
    f_WarehouseName = '';
    certificationNumFilter = '';
    reCheckFilter = '';
    zydhFilter = '';
    f_drawNum = '';
    f_taskNum = '';
    f_SeriousNum;
    selectedWareHouse;
    userDataPermissions;
    // selectedDepts = new LabelValue();
    // selecteddeptarray = new Array<ComboboxItemDto>();
    extensions: Array<ListDtoExtensionsItem>;
    selectedAccounts = new Array<Stock_AccountBaseListDto>();
    useableOptions = [{ label: '全部', value: undefined }, { label: '可用', value: true }];
    isuseable;
    roomOption;
    totalPrice = '0';
    advancedFiltersAreShown = false;
    locationFilter;
    stoveNumFilter;
    selectedValue;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Output() qccard: EventEmitter<any> = new EventEmitter<any>();

    // active = false;
    saving = false;
    closed = false;
    billId = '';
    isShown = false;
    f_CAL_ProductModel;
    purchasetype;
    f_RealInventory;

    ngOnInit(): void {
        this._userDataPermissionServiceProxy.getUserDataPermissions('Warehouses').subscribe(result => {
            this._stockZT_RoomServiceProxy
                .getRoomListByDepts(result.items, '工装库', this._handletype)
                .subscribe(result2 => {
                    this.roomOption = result2.items.map(e => {
                        return {
                            label: e.displayText,
                            value: e
                        };
                    });
                    this.selectedWareHouse = result2.items;
                    this.getBaseAccount();
                });
        });
    }
    show(billid?: string, dept?: string): void {
       
        //  this.active = true;
       
        this.closed = false;
        this.billId = billid;
        this.isuseable = true;
        this.modal.show();
        if (dept) {
            this._stockZT_RoomServiceProxy.getRoomListByDeptsName(dept, '工装库').subscribe(result2 => {
                this.roomOption = result2.items.map(e => {
                    return {
                        label: e.displayText,
                        value: e
                    };
                });
                this.selectedWareHouse = result2.items;
                this.getBaseAccount();
            });
        } else {
            this.getBaseAccount();
        }
    }

    shown(): void {
        this.isShown = true;
        this.getRecordsIfNeeds(null);
    }

    getRecordsIfNeeds(event?: LazyLoadEvent): void {
        if (!this.isShown) {
            return;
        }

        this.getBaseAccount(event);
    }

    close(): void {
        //  this.active = false;
        this.modal.hide();
    }

    onHidden(event) {
        this.closed = true;
    }

    save() {
        this.modalSave.emit(this.selectedValue);
        this.selectedValue = null;

        this.close();
    }
    // getWareHouse() {
    //     this.selecteddeptarray = new Array<ComboboxItemDto>();
    //     this.selecteddeptarray.push(this.selectedDepts);
    //     this._stockZT_RoomServiceProxy.getRoomListByDepts(this.selecteddeptarray, '原材料库', '').subscribe(result2 => {
    //         this.roomOption = result2.items.map(e => {
    //             return {
    //                 label: e.displayText,
    //                 value: e
    //             };
    //         });

    //         this.selectedWareHouse = result2.items;
    //         this.getBaseAccount();
    //     });
    // }
    getBaseAccount(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event) || event.first == undefined) {
            this.paginator.changePage(0);

            return;
        }
        this.primengTableHelper.showLoadingIndicator();

        this._accountService
            .getToolingAccountListExtension(
                this.f_MaterialCode,
                this.f_MaterialName,
                this.f_RealInventory,
                this.f_SeriousNum,
                this.selectedWareHouse,
                this.f_Specification,
                this.f_CAL_ToolingPictureId,
                this.f_CAL_ProductModel,
                // this.selectedDepts.displayText,
                '',
                this.isuseable,
                StockType.工装,
                false,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.extensions = result.items;
                this.primengTableHelper.totalRecordsCount = result.dtos.totalCount;
                this.primengTableHelper.records = result.dtos.items;
                this.primengTableHelper.hideLoadingIndicator();

                this.selectedAccounts = null;
            });
    }
}
